package com.shanzmoo.admin.web.store;

import com.shanzmoo.admin.domain.dto.StoreInfoSaveDTO;
import com.shanzmoo.admin.domain.dto.StoreInfoUpdateDTO;
import com.shanzmoo.base.exception.StoreException;
import com.shanzmoo.core.cache.CurrentUserCache;
import com.shanzmoo.db.store.entity.query.StoreInfoQuery;
import com.shanzmoo.db.store.entity.vo.StoreInfoVO;
import com.shanzmoo.db.store.entity.vo.StoreTreeSelectVO;
import com.shanzmoo.db.store.entity.vo.StoreTreeVO;
import com.shanzmoo.db.store.service.IStoreInfoService;
import com.shanzmoo.base.bean.PageRespBase;
import com.shanzmoo.base.bean.SHttpResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;

import javax.validation.Valid;
import java.sql.SQLIntegrityConstraintViolationException;
import java.util.List;


/**
 * 仓库库位 控制层
 *
 * @author xueqz 2021-03-03
 */
@Api(tags = "仓储-库位")
@RestController
@RequestMapping("/store/info")

public class StoreInfoController {

    @Autowired
    private IStoreInfoService storeInfoService;

    @ApiOperation(value = "分页列表")
    @PostMapping("/pageList")
    public SHttpResponse<PageRespBase<StoreInfoVO>> pageList(@Valid @RequestBody StoreInfoQuery query) {
        // todo 分页查询实现...
        return null;
    }

    @ApiOperation(value = "添加")
    @PostMapping("/add")
    public SHttpResponse<Boolean> add(@Valid @RequestBody StoreInfoSaveDTO saveDto) {
        saveDto.setCreateUser(CurrentUserCache.getUserId());
        try{
            storeInfoService.save(saveDto.converToEntity());
        }catch(DuplicateKeyException e){
            throw new StoreException("仓库编码违反唯一约束");
        }
        return SHttpResponse.ok(Boolean.TRUE);
    }

    @ApiOperation(value = "删除")
    @GetMapping("/delete")
    public SHttpResponse<Boolean> deleteById(@RequestParam(value = "id") Integer id) {
        storeInfoService.deleteById(id);
        return SHttpResponse.ok(Boolean.TRUE);
    }

    @ApiOperation(value = "修改")
    @PostMapping("/update")
    public SHttpResponse<Boolean> update(@Valid @RequestBody StoreInfoUpdateDTO updateDto) {
        updateDto.setUpdateUser(CurrentUserCache.getUserId());
        storeInfoService.updateById(updateDto.converToEntity());
        return SHttpResponse.ok(Boolean.TRUE);
    }

    @ApiOperation(value = "仓库列表树")
    @GetMapping("/storeTreeList")
    public SHttpResponse<List<StoreTreeVO>> storeTreeList() {
        List<StoreTreeVO> storeTreeVOList = storeInfoService.storeTreeList();
        return SHttpResponse.ok(storeTreeVOList);
    }

    @ApiOperation(value = "仓库选择树")
    @GetMapping("/storeTreeSelectList")
    public SHttpResponse<List<StoreTreeSelectVO>> storeTreeSelectList() {
        List<StoreTreeSelectVO> storeTreeSelectVOList = storeInfoService.storeTreeSelectList();
        return SHttpResponse.ok(storeTreeSelectVOList);
    }

    @ApiOperation(value = "大仓选择列表")
    @GetMapping("/firstList")
    public SHttpResponse<List<StoreInfoVO>> firstList() {
        List<StoreInfoVO> re = storeInfoService.getFirstList();
        return SHttpResponse.ok(re);
    }

}
